import { createApp } from 'vue'
import App from './App.vue'
//引入less
import '@/assets/less/index.less'
// import './assets/styles/index.less'
//引入路由
import router from './router'
//全部导入element-plus
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
//全部导入element-plus的图标
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
//引入pinia
import { createPinia } from 'pinia'
//引入mockjs
import '@/api/mock.js'
//引入全局api
import api from '@/api/api'
//引入
import {useAllDataStore} from '@/stores'
function isRoute(to){
  let res=router.getRoutes()
  let resFil=res.filter((item)=>item.path===to.path)
  return resFil.length>0
}
router.beforeEach((to,from)=>{
  if(to.path!=='/login'&&!store.state.token){
    //如果访问的不是登录页，并且没有token
  return {name: '/login'}
  }

  if(!isRoute(to)){
    return {name:'404'}
  }
})
const pinia=createPinia()
const app=createApp(App)
app.config.globalProperties.$api=api
app.use(pinia)
const store=useAllDataStore()
app.use(ElementPlus)
store.addMenu(router,"refresh")
app.use(router).mount('#app')
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

